package com.san.simulation.model.impl;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.impl.bpmn.parser.BpmnParse;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.san.dissertation.bpm.model.Process;
import com.san.dissertation.bpm.model.ProcessPath;
import com.san.dissertation.bpm.model.Task;
import com.san.dissertation.dao.service.impl.DAOServiceImpl;
import com.san.simulation.api.PathResolver;
import com.san.simulation.api.ProcessDeployer;
import com.san.simulation.impl.ActivitiPathResolver;
import com.san.simulation.impl.ActivitiProcessDeployer;
import com.san.simulation.model.api.Element;
import com.san.simulation.model.api.Path;
import com.san.simulation.model.api.ProcessDefinition;

public class ParseProcess {

	private DAOServiceImpl daoServiceImpl;

	public List<ProcessPath> populate() {

		ProcessDeployer deployer = new ActivitiProcessDeployer();
		BpmnParse parse = deployer
				.buildProcessDefinition("makercheckerRevised.bpmn20.xml");
		ProcessDefinition definition = deployer.getProcessDefinitions(parse);

		PathResolver resolver = new ActivitiPathResolver();
		List<Path> paths = resolver.resolvePath(definition);

		System.out.println(paths.size());

		Process process = new Process();

		org.activiti.engine.repository.ProcessDefinition def = ((ActivitiProcessDefinition) definition)
				.getDefinition();
		if (daoServiceImpl.getProcessRepository().findByKey(def.getKey()) != null) {
			return null;
		}

		process.setProcessId(def.getId());
		process.setName(def.getName());
		process.setKey(def.getKey());
		process.setResourceName("makercheckerRevised.bpmn20.xml");

		List<ProcessPath> processPaths = new ArrayList<ProcessPath>();

		int count = 0;

		for (Path pathItem : paths) {
			ProcessPath path = new ProcessPath();
			path.setProcess(process);
			path.setPathId(def.getKey() + ":Path:" + (++count));
			path.setName(path.getPathId());

			List<Task> tasks = new ArrayList<Task>();
			for (Element pathElement : pathItem.getPathElements()) {

				Task task = new Task();
				task.setId(pathElement.getId());
				task.setType(pathElement.getType());
				task.setKey(pathElement.getId());
				task.setName(pathElement.getName());
				task.setSourceId(((ActivitiProcessElement) pathElement)
						.getTransition() != null
						&& pathElement.getSource() != null ? pathElement
						.getSource().getId() : null);
				task.setDestinationId(pathElement.getDestination() != null ? pathElement
						.getDestination().getId() : null);

				tasks.add(task);
			}

			path.setTasks(tasks);
			processPaths.add(path);
		}

		// process.setPath(processPaths);

		System.out.println(process);

		return processPaths;

	}

	/**
	 * @param args
	 */
	public void deploy() {

		List<ProcessPath> paths = populate();
		if (paths != null) {
			daoServiceImpl.saveProcessPath(paths);
		}
	}

	public DAOServiceImpl getDaoServiceImpl() {
		return daoServiceImpl;
	}

	public void setDaoServiceImpl(DAOServiceImpl daoServiceImpl) {
		this.daoServiceImpl = daoServiceImpl;
	}

}
